//会员价值体系
define(function(require){
	var $=require("jq");
	var Dialog=require("pz_use_dialog.js");
	//J_tabs标签页切换
	function J_tabs(tab){
		var __this__=this;
		//保存独立的tab对象
		this.tab=tab;
		//保存选项卡
		this.tabBtns=$(".tabs-th li",tab);
		//保存选项卡对应的内容
		this.cons=$(".tabs-bd-con",tab);
		//添加事件
		this.tabBtns.click(function(){
			//设置选项卡样式
			__this__.setTabStyle($(this));
		});
	};
	J_tabs.prototype={
		setTabStyle:function(obj){
				if(!obj.hasClass("tab-actived")){
					var index=obj.index();
					obj.addClass("tab-actived").siblings().removeClass("tab-actived");
					this.cons.eq(index).addClass("tab-con-show").siblings().removeClass("tab-con-show");
				};
		}
	};
	J_tabs.init=function(tabs){
		var _this_=this;	
		tabs.each(function(){
			new _this_(this);	
		});
	};
	window["J_tabs"]=J_tabs;
	//进度条
	function J_Progress(progress){
		 var _this_=this;
		 //保存进度条对象
		 this.progress=progress;
		 //保存进度列表
		 this.progressList=$(".progress-desc li",progress);
		 //保存进度条
		 this.progressSlideRoad=$(".progress-step",progress); 
		 //保存滑块
		 this.progresSlider=$(".progress-slider",progress);
		 this.progresSliderSpan=this.progresSlider.find("span");
		 //保存参数值
		 this.progressStepValue=parseInt(this.progress.attr("data-step"));
		 this.progressCurValue=parseInt(this.progress.attr("data-cur-value"));
		 //获取当前映射对象的宽度和距离父元素的偏移值
		 this.currentStepNodeWidth=this.progressList.eq(this.progressStepValue-1).width();
		 this.currentStepNodeOffsetLeft=this.progressList.eq(this.progressStepValue-1).position().left;
		 //获取当前滑块最近的值
		 this.currentStepValue=parseInt(this.progressList.eq(this.progressStepValue-1).attr("date-step-value"));
		 //定义一个比例值
		 this.bili=1-(this.currentStepValue-this.progressCurValue)/this.currentStepValue;
		 if(this.progressStepValue==1){
		 	this.addWidth=this.currentStepNodeWidth*this.bili;
		 }else{
		 	this.addWidth=this.currentStepNodeWidth*this.bili+this.currentStepNodeOffsetLeft;
			this.addWidth=this.addWidth>500?450:this.addWidth;
		 };
		 this.addWidth=Math.ceil(this.addWidth);
		 //设置进度条
		 this.setProgressWidth();
		 //获取间隔节点
		 this.hoverNodes=this.progressList.find("span");
		 //获取升级时间节点
		 this.stepDateNode=$(".progress-step-date",progress);
		 //获取升级节点内的em
		 this.stepDateEm=$("em",this.stepDateNode);
		 //添加鼠标事件
		 this.hoverNodes.hover(function(){
		 	_this_.mouseOver(this);
		 },function(){
			_this_.stepDateNode.hide();
		 });
	};
	J_Progress.prototype={
		//设置进度条宽度
		setProgressWidth:function(){
			var _this=this;
			if(this.progressCurValue>=0&&this.progressCurValue<=48000){
				//将映射对象的宽度和偏移值相加
				this.progressSlideRoad.css("width",this.addWidth);
				//设置滑块和设置当前名望值到滑块里
				this.progresSlider.find("span").text(this.progressCurValue);
				this.progresSlider.animate({"left":this.addWidth-11}).find("b").css("left","6px");
			}else if(this.progressCurValue>48000){
				//将映射对象的宽度和偏移值相加
				this.progressSlideRoad.animate({width:this.addWidth},1000);
				//设置滑块和设置当前名望值到滑块里
				this.progresSlider.find("span").text(this.progressCurValue);
				this.progresSlider.animate({left:this.addWidth-30},1000);
				var totalWidth=this.addWidth;
				var curSpanValueNode=this.progresSlider.find("span");
				var timer=window.setInterval(function(){
						//document.title=totalWidth+"---"+_this.progressSlideRoad.width();
						if(totalWidth==_this.progressSlideRoad.width()){
							window.clearInterval(timer);
							//当运动结束时直接设置当前值到这里
							curSpanValueNode.text(_this.progressCurValue);
						};
						curSpanValueNode.text(Math.floor(_this.progressSlideRoad.width()/totalWidth*_this.progressCurValue));
					},50);
			};	
		},
		//添加时间轴效果
		mouseOver:function(o){
			//设置时间节点
			this.stepDateEm.html($(o).attr("data-date")+"<br />"+$(o).attr("data-name"));
			//设置位置
			var left=$(o).parent().index();
			switch(left){
				case 1:
				left="8px";
				break;
				case 2:
				left="68px";
				break;
				case 3:
				left="138px";
				break;		
				case 4:
				left="218px";
				break;
				case 5:
				left="318px";
				break;
				case 0:
				left="-15px";
				break;
			};
			if(left!="-15px"){
				this.stepDateNode.css("left",left).show();
				this.stepDateNode.find("i").css("left","50%");
				this.stepDateEm.css("textAlign","center");
			}else{
				this.stepDateNode.css("left",left).show();
				this.stepDateNode.find("i").css("left",16);
				this.stepDateEm.css("textAlign","left");
			};
		}
	};
	J_Progress.init=function(progress){	
		var _this_=this;
		progress.each(function(){
			new _this_($(this));
		});	
	};
	//J_Progress
	//J_Progress.init($(".J_Progress"))
	//window["J_Progress"]=J_Progress;

	$(function(){
		//初始化tab
		J_tabs.init($(".J_tabs"));
		//获取进度条
		var progress=$(".J_Progress").eq(0);
		var progress_step;
		var progress_value=parseInt(progress.attr("data-cur-value"));
		if(progress_value<=98000){
			progress_step=1;
		}else if(progress_value>98000&&progress_value<=300000){
			progress_step=2;
		}else if(progress_value>300000&&progress_value<=3000000){
			progress_step=3;
		}else if(progress_value>300000&&progress_value<=5000000){
			progress_step=4;
		}else if(progress_value>5000000&&progress_value<=10000000){
			progress_step=5;
		}else{
			progress_step=5;
		};
		//设置步骤
		progress.attr("data-step",progress_step);
		//初始化进度条
		J_Progress.init(progress);
		
		
		/*******标出星星********/
		var activeBtn=$(".active").eq(0);
		activeBtn.click(function(){
			if(!$(this).hasClass("active-ok")){
				var prompt=new PZ_Dialog({
					type:"prompt",
					text:"请输入激活码",
					buttonSureText:"激活",
					buttonCancelText:"放弃",
					callBack:function(value){
						$.post("/vipCenter/activeUser.do", {activeCode:value}, function(msg){
							var tips=new Dialog({
								  type:"tips",
								  text:msg.message,
								  delay:1,			//提示框延时几秒，不提供默认为3秒关闭
								  mask:"transparent"
								  });
							if(msg.ret==1) {
								//禁用激活按钮
								activeBtn.addClass("active-ok").text("你已激活");
								window.location.href="/vipCenter/myLevel.do?menu_id=24";
							}
						}, "json");
					},
					mask:0.3,
					isDrag:true
				});
			};
		});
		
		var module_table=$(".module-table").eq(0),
			level=parseInt(module_table.attr("data-level"));
		for(var i =1;i<level;i++){
			module_table.addClass("vip-star-into-"+i);
		};
	});
	
	
	//赠送网址操作
	var giveFameBtns = $("#selectPage_A");
	
	giveFameBtns.delegate("a.js-fame","click",function(){
		if($(this).attr("data-isgive") === "yes"){
			var giveOrderId = $(this).attr("data-orderId");
			var famePopDialog = new Dialog({
				type:"confirm",
				height:200,
				text:"赠送名望值",
				tipsText:"<div id='js-user-fame' class='user-fame-area'><div><label for='user-fame-name'>用户名 :</label><input type='text' id='user-fame-name' name='user-fame-name'></div><div><label for='user-fame-value'>名望值 :</label><input type='text' id='user-fame-value' name='user-fame-value'></div><p><s></s>赠送名望值后不可赠回或转赠，请谨慎操作！</p></div>",
				buttonSureText:"确定赠送",
				buttonCancelText:"放弃",
				callBack:function(value){
					//赠送对方用户名相关验证
					var userFameName  = $("#user-fame-name").val(),
						userFameValue = $("#user-fame-value").val();
					if($.trim(userFameName)==""){
						alert("请输入用户名！");
						return true;
					};
					if($.trim(userFameValue)==""){
						
						
						
						alert("请输入名望值！");
						return true;	
					}else{
						if(isNaN(parseInt(userFameValue))){
							alert("赠送名望值必须是数字！");
							return true;
						};
						
					};
					
					$.post("/crm/fameTransfer.do",{orderId:giveOrderId,toLoginId:userFameName,fameValue:userFameValue},function(ret){
						
//						if(!ret.success)
						alert(ret.msg);
						window.location.reload();
					},"json");
					
					
				},
				mask:0.3,
				isDrag:true
			});
		};
	});
	
	
	
	
	
	
	
	
	
});